import { defineStore } from 'pinia';
import piniaPersistConfig from './piniaPersist';
import { User } from '@/api/types';
import { getUserInfo } from '@/api/user';

export interface GlobalTypes {
	token: string;
	userInfo: User.SysUser;
	currentRoute: string;
	menuList: [];
	menuCollapse: boolean;
	showBreadcrumb: boolean;
}

export const GlobalStore = defineStore({
	id: 'GlobalStore',
	state: (): GlobalTypes =>
		<GlobalTypes>{
			// token
			token: '',
			// userInfo
			userInfo: {},
			currentRoute: '',
			menuList: [],
			menuCollapse: false,
			showBreadcrumb: true,
		},
	getters: {},
	actions: {
		// setToken
		setToken(token: string) {
			this.token = token;
		},
		setRoute(val: string) {
			this.currentRoute = val;
		},
		async getUserInfoFromServer() {
			const { body } = await getUserInfo();
			this.userInfo = body;
		},
		resetUserInfo() {
			this.setToken('');
			this.userInfo = {
				emailAddress: '',
				id: 0,
				isEnabled: 0,
				lastLoginIpAddr: '',
				lastLoginTime: '',
				realName: '',
				avatar: '',
				phoneNumber: '',
				userName: '',
			};
		},
	},
	persist: piniaPersistConfig('GlobalStore'),
});
